/* $RCSFile: ADNDynSystemUser.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynSystemUser.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Globalization;

using AddonNice.Settings;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
   public  class ADNDynSystemUser : DynEntity
   {
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.systemuser)
            {}

            public override Type DynType
            { get { return typeof(ADNDynSystemUser); } }

            public override string DynTypename
            {   get{ return CrmStr.systemuser;}}

            public override DynEntity Build()
            {   return new ADNDynSystemUser();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynSystemUser(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynSystemUser(w, PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynSystemUser(w,PrimaryKeyValue);}
        }

        static ADNDynSystemUser(){new Ftor();} 

	    /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynSystemUser(): base()
		{}

        public ADNDynSystemUser(CRMWrapperBase w, string PrimaryFiedlValue)
           : base(w, CrmStr.systemuser)
        {
	        fullname	 = PrimaryFiedlValue;
        }

        public ADNDynSystemUser(CRMWrapperBase w, Guid PrimaryKeyValue)
           : base(w,CrmStr.systemuser,PrimaryKeyValue)
        {}

        public ADNDynSystemUser(CRMWrapperBase w, InternalDynamicEntity e)
           : base(w,CrmStr.systemuser,e,null,null)
        {}

        public ADNDynSystemUser(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(w,CrmStr.systemuser,e,prop,s)
        {}
   		
        public ADNDynSystemUser(ResetWrapperHandler fn, string PrimaryFiedlValue)
           : base(fn, CrmStr.systemuser)
        {
	        fullname	 = PrimaryFiedlValue;
        }

        public ADNDynSystemUser(ResetWrapperHandler fn, Guid PrimaryKeyValue)
           : base(fn,CrmStr.systemuser,PrimaryKeyValue)
        {}

        public ADNDynSystemUser(ResetWrapperHandler fn, InternalDynamicEntity e)
           : base(fn,CrmStr.systemuser,e,null,null)
        {}

        public ADNDynSystemUser(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
           : base(fn,CrmStr.systemuser,e,prop,s)
        {}

       public override int ObjectTypeCode
	    {
			get
			{ return (int)ADNTypeCodes.SystemUser; }
            set
            { }
		}

   		public override ADNTypeCodes ADNTypeCode
		{
			get
			{ return ADNTypeCodes.SystemUser; }
		}

        [CrmDynAttribute(CrmStr.systemuser)]
   		public Guid systemuserid
        {
            get
            {
				return KeyPropertyValue(CrmStr.systemuserid);
            }
            set
            {
                AddKeyProperty(CrmStr.systemuserid,value);
            }
        }
        
       [CrmDynAttribute(CrmStr.systemuser)]
        public string lastname
        {
            get
            {
				return StringPropertyValue(CrmStr.lastname);
            }
            set
            {
                AddStringProperty(CrmStr.lastname,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string firstname
        {
            get
            {
				return StringPropertyValue(CrmStr.firstname);
            }
            set
            {
                AddStringProperty(CrmStr.firstname,value);
            }
        }

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.systemuserid) || KeyPropertyValue(CrmStr.systemuserid) != crmId )
                systemuserid    =   crmId;
        }
		
        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return systemuserid;
           }
           set
           {
               systemuserid    =   value;
           }
        }

        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.fullname;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return fullname;
            }
            set
            {
                fullname =   value;
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
       public string nickname
        {
            get
            {
				return StringPropertyValue(CrmStr.nickname);
            }
            set
            {
                AddStringProperty(CrmStr.nickname,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string middlename
        {
            get
            {
				return StringPropertyValue(CrmStr.middlename);
            }
            set
            {
                AddStringProperty(CrmStr.middlename,value);
            }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
   		public Guid businessunitid
        {
            get
            {
				return LookupPropertyValue(CrmStr.businessunitid);
            }
            set
            {
                AddLookupProperty(CrmStr.businessunitid,CrmStr.businessunit,value);
            }
        }

   		public string businessunitName
        {
            get
            {
                if ( ContainsKey(CrmStr.businessunitid) )
				    return LookupPropertyName(CrmStr.businessunitid);
                return string.Empty;
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string jobtitle
        {
            get
            {
				return StringPropertyValue(CrmStr.jobtitle);
            }
            set
            {
                AddStringProperty(CrmStr.jobtitle,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string mobilephone
        {
            get
            {
				return StringPropertyValue(CrmStr.mobilephone);
            }
            set
            {
                AddStringProperty(CrmStr.mobilephone,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string domainname
        {
            get
            {
				return StringPropertyValue(CrmStr.domainname);
            }
            set
            {
                AddStringProperty(CrmStr.domainname,value);
            }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public int accessmode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.accessmode);
			}
			set
			{
				AddPicklistProperty(CrmStr.accessmode,value);
			}
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string internalemailaddress
        {
            get
            {
				return StringPropertyValue(CrmStr.internalemailaddress);
            }
            set
            {
                AddStringProperty(CrmStr.internalemailaddress,value);
            }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public int incomingemaildeliverymethod
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.incomingemaildeliverymethod);
			}
			set
			{
				AddPicklistProperty(CrmStr.incomingemaildeliverymethod,value);
			}
        }

        static public ADNDynSystemUser GetUserByDomainName(CRMWrapperBase Wrapper,string domainName)
        {
            return Wrapper.Util.FindSystemUserByDomainName(domainName);
        }

        #region address1

        [CrmDynAttribute(CrmStr.systemuser)]
        public int address1_addresstypecode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.address1_addresstypecode);
			}
			set
			{
				AddPicklistProperty(CrmStr.address1_addresstypecode,value);
			}
		}
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public int address1_shippingmethodcode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.address1_shippingmethodcode);
			}
			set
			{
				AddPicklistProperty(CrmStr.address1_shippingmethodcode,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_name
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_name);
			}
			set
			{
				AddStringProperty(CrmStr.address1_name,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_telephone1
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_telephone1);
			}
			set
			{
				AddStringProperty(CrmStr.address1_telephone1,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_telephone2
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_telephone2);
			}
			set
			{
				AddStringProperty(CrmStr.address1_telephone2,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_telephone3
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_telephone3);
			}
			set
			{
				AddStringProperty(CrmStr.address1_telephone3,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_fax
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_fax);
			}
			set
			{
				AddStringProperty(CrmStr.address1_fax,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_line1
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_line1);
			}
			set
			{
				AddStringProperty(CrmStr.address1_line1,value);
			}
		}
		
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_line2
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_line2);
			}
			set
			{
				AddStringProperty(CrmStr.address1_line2,value);
			}
		}
		
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_line3
		{
			get
			{
				return StringPropertyValue(CrmStr.address1_line3);
			}
			set
			{
				AddStringProperty(CrmStr.address1_line3,value);
			}
		}
        
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_country
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_country);
            }
            set
            {
				AddStringProperty(CrmStr.address1_country,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_county
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_county);
            }
            set
            {
				AddStringProperty(CrmStr.address1_county,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_postalcode
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_postalcode);
            }
            set 
             {
				AddStringProperty(CrmStr.address1_postalcode,value);
              }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_city
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_city);
            }
            set 
             {
				AddStringProperty(CrmStr.address1_city,value);
             }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_stateorprovince
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_stateorprovince);
            }
            set 
             {
				AddStringProperty(CrmStr.address1_stateorprovince,value);
             }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address1_upszone
        {
            get
            {
				return StringPropertyValue(CrmStr.address1_upszone);
            }
            set 
             {
				AddStringProperty(CrmStr.address1_upszone,value);
             }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public double address1_longitude
        {
            get
            {
				return DoublePropertyValue(CrmStr.address1_longitude);
            }
            set 
             {
				AddFloatProperty(CrmStr.address1_longitude,value);
             }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public double address1_latitude
        {
            get
            {
				return DoublePropertyValue(CrmStr.address1_latitude);
            }
            set 
             {
				AddFloatProperty(CrmStr.address1_latitude,value);
             }
        }
        #endregion address1
   
        #region address2

        [CrmDynAttribute(CrmStr.systemuser)]
        public int address2_addresstypecode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.address2_addresstypecode);
			}
			set
			{
				AddPicklistProperty(CrmStr.address2_addresstypecode,value);
			}
		}
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public int address2_shippingmethodcode
		{
			get
			{
				return PicklistPropertyIntValue(CrmStr.address2_shippingmethodcode);
			}
			set
			{
				AddPicklistProperty(CrmStr.address2_shippingmethodcode,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_name
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_name);
			}
			set
			{
				AddStringProperty(CrmStr.address2_name,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_telephone1
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_telephone1);
			}
			set
			{
				AddStringProperty(CrmStr.address2_telephone1,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_telephone2
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_telephone2);
			}
			set
			{
				AddStringProperty(CrmStr.address2_telephone2,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_telephone3
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_telephone3);
			}
			set
			{
				AddStringProperty(CrmStr.address2_telephone3,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_fax
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_fax);
			}
			set
			{
				AddStringProperty(CrmStr.address2_fax,value);
			}
		}

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_line1
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_line1);
			}
			set
			{
				AddStringProperty(CrmStr.address2_line1,value);
			}
		}
		
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_line2
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_line2);
			}
			set
			{
				AddStringProperty(CrmStr.address2_line2,value);
			}
		}
		
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_line3
		{
			get
			{
				return StringPropertyValue(CrmStr.address2_line3);
			}
			set
			{
				AddStringProperty(CrmStr.address2_line3,value);
			}
		}
        
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_country
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_country);
            }
            set
            {
				AddStringProperty(CrmStr.address2_country,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_county
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_county);
            }
            set
            {
				AddStringProperty(CrmStr.address2_county,value);
            }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_postalcode
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_postalcode);
            }
            set 
             {
				AddStringProperty(CrmStr.address2_postalcode,value);
              }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_city
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_city);
            }
            set 
             {
				AddStringProperty(CrmStr.address2_city,value);
             }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_stateorprovince
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_stateorprovince);
            }
            set 
             {
				AddStringProperty(CrmStr.address2_stateorprovince,value);
             }
        }
       
        [CrmDynAttribute(CrmStr.systemuser)]
        public string address2_upszone
        {
            get
            {
				return StringPropertyValue(CrmStr.address2_upszone);
            }
            set 
             {
				AddStringProperty(CrmStr.address2_upszone,value);
             }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public double address2_longitude
        {
            get
            {
				return DoublePropertyValue(CrmStr.address2_longitude);
            }
            set 
             {
				AddFloatProperty(CrmStr.address2_longitude,value);
             }
        }

        [CrmDynAttribute(CrmStr.systemuser)]
        public double address2_latitude
        {
            get
            {
				return DoublePropertyValue(CrmStr.address2_latitude);
            }
            set 
             {
				AddFloatProperty(CrmStr.address2_latitude,value);
             }
        }
         #endregion address2

        #region Role testing

        public bool IsInCRMRole(string role2Test)
        {
            if ( CRMTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSystemUser IsInCRMRole BEG role2Test: {0}.",role2Test),CRMTraceSwitch.Sw.Info);
			bool ret	=	wrapper.IsInCRMUserRoles(role2Test,systemuserid);
			if ( CRMTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSystemUser IsInCRMRole END role: {0}, ret: {1}", role2Test, ret),CRMTraceSwitch.Sw.Info);
            return ret;
        }

		public bool IsUserInRoles(string roles) 
		{
			if ( string.IsNullOrEmpty(roles) )
				return false; 
            bool ret						= false;
            
            string DBAllUser				= Config.InternallAllUsersRole;
            string CRMAdmin					= Config.CRMAdminRole;
            string internalAdminRole		= Config.InternalAdminRole;
            foreach (string splitRole in roles.Split(new char[] { ';' },StringSplitOptions.RemoveEmptyEntries)) 
			{
				string role					= splitRole.Trim();
                if ( string.IsNullOrEmpty(role) )
                    continue;
                if (role == internalAdminRole )
                    role					= CRMAdmin;
                if (role == DBAllUser || IsInCRMRole(role))
                {
                    ret						= true;
                    break;
                }
			}
            if ( CRMTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSystemUser IsUserInroles END roles: '{0}', ret: {1}", roles, ret),CRMTraceSwitch.Sw.Info);
            return ret;
		}


        #endregion Role testing 

        #region Utilities

        public static ADNDynSystemUser FindSystemUserByFirstLastName(CRMWrapperBase Wrapper,string firstName,string lastName,bool CreateIfNotFound,ref bool created)
        {
            bool created2       =   created;
            ADNDynSystemUser dynC     =   Wrapper.Util.FindSystemUserByFirstLastName(firstName,lastName, CreateIfNotFound,ref created2);
            created             =   created2;
            return dynC;
        }

        public static ADNDynSystemUser FindSystemUserByName(CRMWrapperBase Wrapper,string fullName,bool CreateIfNotFound,ref bool created)
        {
            bool created2       =   created;
            ADNDynSystemUser dynC     =   Wrapper.Util.FindSystemUserByName(fullName, CreateIfNotFound,ref created2);
            created             =   created2;
            return dynC;
        }
        public static ADNDynSystemUser FindSystemUserByDomainName(CRMWrapperBase Wrapper,string domainName,bool All)
        {
            ADNDynSystemUser dynC     =   Wrapper.Util.FindSystemUserByDomainName(domainName, All);
            return dynC;
        }

        #endregion Utilities
   
   
   }
}